//
//  PersonCell.m
//  Assistant
//
//  Created by Hunk on 12-3-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "PersonCell.h"
#import <QuartzCore/QuartzCore.h>

@implementation PersonCell
@synthesize person   = _person;
@synthesize delegate = _delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
    {
        // Person photo
        _photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_photoButton setFrame:CGRectMake(10.0, 1.0, 44.0, 44.0)];
        _photoButton.layer.cornerRadius = CORNERRADIUS;
        _photoButton.layer.masksToBounds = YES;
        [_photoButton addTarget:self 
                         action:@selector(photoButtonHandle:) 
               forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_photoButton];
        
        // Person name label
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(60.0, 0.0, 200, 46.0)];
        [_nameLabel setBackgroundColor:[UIColor clearColor]];
        [_nameLabel setFont:[UIFont boldSystemFontOfSize:18]];
        [_nameLabel setTextAlignment:UITextAlignmentLeft];
        [self addSubview:_nameLabel];
        
//        // Accessory view
//        UIImage *accessoryImage = [UIImage imageNamed:@"accessory.png"];
//        UIImageView *accessoryImageView = [[UIImageView alloc] initWithFrame:CGRectMake(_nameLabel.frame.size.width, (_nameLabel.frame.size.height - accessoryImage.size.height) / 2.0, accessoryImage.size.width, accessoryImage.size.height)];
//        [accessoryImageView setBackgroundColor:[UIColor clearColor]];
//        [accessoryImageView setImage:accessoryImage];
//        [self addSubview:accessoryImageView];
//        [accessoryImageView release];
    }
    return self;
}

#pragma mark Set person
- (void)setPerson:(AddressBookPerson *)person
{
    if(_person != person)
    {
        [_person release];
        
        _person = [person retain];
    }
    
    UIImage *personPhoto = [person personPhoto];
    if(nil != personPhoto)
    {
        [_photoButton setImage:personPhoto forState:UIControlStateNormal];
    }
    else
    {
        [_photoButton setImage:[UIImage imageNamed:@"icon_check.png"] forState:UIControlStateNormal];
    }
    
    [_nameLabel setText:[_person personName]];
}

#pragma mark Set delegate
- (void)setDelegate:(id<PersonCellDelegate>)delegate
{
    if(_delegate != delegate)
    {
        [_delegate release];
        
        _delegate = [delegate retain];
    }
}

#pragma mark Photo button handle
- (void)photoButtonHandle:(id)sender
{
    if(_delegate && [_delegate respondsToSelector:@selector(personCell:didSelectedPerson:)])
    {
        [_delegate personCell:self didSelectedPerson:_person];
    }
}

- (void)dealloc
{
    SAFE_RELEASE(_person)
    SAFE_RELEASE(_nameLabel)
    SAFE_RELEASE(_delegate)
    
    [super dealloc];
}

@end
